home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / OS / FWMemory / Include / FWMemMgr.h < prev    next >
Encoding:
Text File  |  1994-04-21  |  12.8 KB  |  382 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWMemMgr.h
  4. //    Release Version:    $ 1.0d1 $
  5. //
  6. //    Creation Date:        3/28/94
  7. //
  8. //    Copyright:    © 1993, 1994 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #ifndef FWMEMMGR_H
  13. #define FWMEMMGR_H
  14.  
  15. #include <stddef.h>
  16.  
  17. #ifndef FWSTDDEF_H
  18. #include "FWStdDef.h"
  19. #endif
  20.  
  21. #ifndef FWPRIEXC_H
  22. #include "FWPriExc.h"
  23. #endif
  24.  
  25. #ifndef FWEXCDEF_H
  26. #include "FWExcDef.h"
  27. #endif
  28.  
  29. #if defined(FW_BUILD_MAC) && !defined(__MEMORY__)
  30. #include <Memory.h>
  31. #endif
  32.  
  33. #if defined(FW_BUILD_WIN32S) && !defined(_INC_WINDOWS)
  34. #include <Windows.h>
  35. // API macro that conflicts with a method in FW_CMemoryManager
  36. // Why Microsoft could not do this with inlines, remains a mystery to this date...
  37. #undef CopyMemory
  38. #endif
  39.  
  40. //========================================================================================
  41. // Forward class declarations
  42. //========================================================================================
  43.  
  44. class FW_CMemoryHeap;
  45.  
  46. //========================================================================================
  47. // Type definitions
  48. //========================================================================================
  49.  
  50. typedef void (*FW_PFVV)();    
  51.     // Pointer to function returning void.
  52.     // Used with set_new_handler.  See ARM, pp 280-81.
  53.  
  54. //========================================================================================
  55. // Global procedure definitions
  56. //========================================================================================
  57.  
  58. #ifdef FW_NEW_AND_DELETE_NOT_DISABLED
  59.  
  60. // Currently parts must use OpenDoc's new and delete to avoid mismatching new and delete
  61. // calls in certain situations. Will be fixed soon.
  62.  
  63. #ifdef __BORLANDC__
  64.   void*   cdecl operator new(size_t size);
  65.   void    cdecl operator delete(void* block);
  66. #else
  67.   void*         operator new(size_t size);
  68.   void          operator delete(void* block);
  69. #endif
  70.  
  71. #endif
  72.  
  73. FW_PFVV set_new_handler(FW_PFVV handler);
  74.     // See ARM, pp 280-81.
  75.  
  76. #ifdef FW_BUILD_MAC
  77. pascal long MacGrowZoneProc(Size /* cbNeeded */);
  78.  
  79. void SetStackSpace(long numBytes);
  80.  
  81. pascal Ptr GetCurrentStackBase() = {    /*  kMoveLAbsolute                */    0x2eb8,            
  82.                                          /*  CurStackBase                */     0x0908 };
  83.                                         /*  MOVE.L CurStackBase,(SP)    */
  84. #endif
  85.  
  86. //========================================================================================
  87. // CLASS FW_CMemoryManager
  88. //========================================================================================
  89.  
  90. class FW_CMemoryManager
  91. {
  92. public:
  93.  
  94.     // Utility routines for manipulating blocks of memory
  95.     static void CopyMemory(const void* const source,
  96.                            void* const destination,
  97.                            unsigned long bytesToMove);
  98.     static void SetMemory(void* aBlock,
  99.                           unsigned long bytesToSet,
  100.                           unsigned char byteValue);
  101.     static void* AddOffsetToPointer(void* pointer,
  102.                                     unsigned long Offset);
  103.  
  104.     // when resizable, pointer-based blocks are needed:
  105.     static void* AllocateBlock(unsigned long bytesRequested);
  106.     static void* ResizeBlock(void* aBlock,
  107.                              unsigned long bytesRequested);
  108.     static void FreeBlock(void* aBlock);
  109.     static unsigned long GetBlockSize(void* aBlock);
  110.  
  111.     // when platform specific handles are needed:
  112.     static FW_PlatformHandle AllocateSystemHandle(unsigned long bytesRequested);
  113.     static FW_PlatformHandle ResizeSystemHandle(FW_PlatformHandle aHandle,
  114.                                                 unsigned long bytesRequested);
  115.     static void FreeSystemHandle(FW_PlatformHandle aHandle);
  116.     static void* LockSystemHandle(FW_PlatformHandle aHandle);
  117.     static void UnlockSystemHandle(FW_PlatformHandle aHandle);
  118.     static unsigned long GetSystemHandleSize(FW_PlatformHandle aHandle);
  119.     static FW_PlatformHandle CopySystemHandle(FW_PlatformHandle aHandle);
  120.  
  121.     //internal methods
  122.     // when resizable, pointer-based blocks are needed, no debug checking:
  123.     static void* PrimitiveAllocateBlock(unsigned long bytesRequested);
  124.     static void* PrimitiveResizeBlock(void* aBlock,
  125.                                       unsigned long bytesRequested);
  126.     static void PrimitiveFreeBlock(void* aBlock);
  127.     static unsigned long PrimitiveGetBlockSize(void* aBlock);
  128.  
  129.     static void InitializeRawBlock(void* aBlock,
  130.                                    unsigned long sizeBlock);
  131.  
  132.     static void DefaultNewHandler();
  133.  
  134. private:
  135.     static FW_CMemoryHeap* GetMemoryHeap();
  136.  
  137.     FW_CMemoryManager() {};
  138.         // abstract class.  Note that all methods are statics.
  139. };
  140.  
  141.  
  142. //========================================================================================
  143. // CLASS FW_XMemoryExhausted
  144. //
  145. //    An exception class for free store exhausted errors.
  146. //========================================================================================
  147.  
  148. // Work In Progress: ???JEL
  149. // Some names are too long.  I suggest the following name changes:
  150. //
  151. //        fSizeRequestedAvailability -> fHasSizeRequested
  152. //        SizeRequestedAvailability -> FW_Boolean
  153. //
  154. //    See the inline for FW_XMemoryExhausted::HasSizeRequested below
  155.  
  156. class FW_XMemoryExhausted : public FW_XPrivException
  157. {
  158. public:
  159.     enum SizeRequestedAvailability
  160.     {
  161.         kSizeRequestedNotAvailable,
  162.         kSizeRequestedAvailable
  163.     };
  164.     FW_XMemoryExhausted();
  165.     FW_XMemoryExhausted(unsigned long requestedSize);
  166.     FW_XMemoryExhausted(const FW_XMemoryExhausted& exception);
  167.     virtual ~FW_XMemoryExhausted();
  168.     unsigned long GetRequestedSize() const;
  169.     SizeRequestedAvailability HasSizeRequested() const;
  170.  
  171.     _FW_EXCEPTION_DEFINE(FW_XMemoryExhausted)
  172. private:
  173.     const unsigned long fRequestedSize;
  174.     const SizeRequestedAvailability fSizeRequestedAvailability;
  175. };
  176.  
  177. //========================================================================================
  178. // FW_XMemoryExhausted inline member functions
  179. //========================================================================================
  180.  
  181. //----------------------------------------------------------------------------------------
  182. //    FW_XMemoryExhausted::FW_XMemoryExhausted
  183. //----------------------------------------------------------------------------------------
  184.  
  185. inline FW_XMemoryExhausted::FW_XMemoryExhausted() :
  186.     FW_XPrivException(),
  187.     fSizeRequestedAvailability(kSizeRequestedNotAvailable),
  188.     fRequestedSize(0)
  189. {
  190. }
  191.  
  192. //----------------------------------------------------------------------------------------
  193. //    FW_XMemoryExhausted::FW_XMemoryExhausted
  194. //----------------------------------------------------------------------------------------
  195.  
  196. inline FW_XMemoryExhausted::FW_XMemoryExhausted(unsigned long requestedSize) :
  197.     FW_XPrivException(),
  198.     fSizeRequestedAvailability(kSizeRequestedAvailable),
  199.     fRequestedSize(requestedSize)
  200. {
  201. }
  202.  
  203. //----------------------------------------------------------------------------------------
  204. //    FW_XMemoryExhausted::FW_XMemoryExhausted
  205. //----------------------------------------------------------------------------------------
  206.  
  207. inline FW_XMemoryExhausted::FW_XMemoryExhausted(const FW_XMemoryExhausted& exception) :
  208.     FW_XPrivException(),
  209.     fSizeRequestedAvailability(exception.fSizeRequestedAvailability),
  210.     fRequestedSize(exception.fRequestedSize)
  211. {
  212. }
  213.  
  214. //----------------------------------------------------------------------------------------
  215. //    FW_XMemoryExhausted::GetRequestedSize
  216. //----------------------------------------------------------------------------------------
  217.  
  218. inline unsigned long FW_XMemoryExhausted::GetRequestedSize() const
  219. {
  220.     return fRequestedSize;
  221. }
  222.  
  223. //----------------------------------------------------------------------------------------
  224. //    FW_XMemoryExhausted::HasSizeRequested
  225. //----------------------------------------------------------------------------------------
  226.  
  227. inline FW_XMemoryExhausted::SizeRequestedAvailability FW_XMemoryExhausted::HasSizeRequested() const
  228. {
  229.     return fSizeRequestedAvailability;
  230. }
  231.  
  232. //========================================================================================
  233. // CLASS FW_CAcquireLockedSystemHandle
  234. //
  235. //    A resource acquisition helper object for locking a system handle within a scope.
  236. //========================================================================================
  237.  
  238. template<class tItem>
  239. class FW_CAcquireLockedSystemHandle : public _FW_CAutoDestructObject
  240. {
  241. public:
  242.     FW_CAcquireLockedSystemHandle(FW_PlatformHandle aSystemHandle);
  243.     virtual ~FW_CAcquireLockedSystemHandle();
  244.     
  245.     tItem* GetPointer() const;
  246.     
  247. private:
  248.     FW_CAcquireLockedSystemHandle(const FW_CAcquireLockedSystemHandle<tItem>& acquireObject);
  249.     FW_CAcquireLockedSystemHandle<tItem>& operator=(const FW_CAcquireLockedSystemHandle<tItem>& acquireObject);
  250.         // Copy constructor and assignment operator not valid for this class.
  251.  
  252.     FW_PlatformHandle    fLockedSystemHandle;
  253.     tItem*     fLockedPointer;
  254. };
  255.  
  256. //----------------------------------------------------------------------------------------
  257. // FW_CAcquireLockedSystemHandle::FW_CAcquireLockedSystemHandle
  258. //----------------------------------------------------------------------------------------
  259. template<class tItem>
  260. FW_CAcquireLockedSystemHandle<tItem>::FW_CAcquireLockedSystemHandle(FW_PlatformHandle aSystemHandle)
  261.                      : fLockedSystemHandle(aSystemHandle), 
  262.                        fLockedPointer((tItem*)FW_CMemoryManager::LockSystemHandle(aSystemHandle))
  263. {
  264.     FW_END_CONSTRUCTOR
  265. }
  266.  
  267. //----------------------------------------------------------------------------------------
  268. // FW_CAcquireLockedSystemHandle::~FW_CAcquireLockedSystemHandle
  269. //----------------------------------------------------------------------------------------
  270. template<class tItem>
  271. FW_CAcquireLockedSystemHandle<tItem>::~FW_CAcquireLockedSystemHandle()
  272. {
  273.     FW_START_DESTRUCTOR
  274.     FW_CMemoryManager::UnlockSystemHandle(fLockedSystemHandle);
  275. }
  276.  
  277. //----------------------------------------------------------------------------------------
  278. // FW_CAcquireLockedSystemHandle::GetPointer
  279. //----------------------------------------------------------------------------------------
  280. template<class tItem>
  281. tItem* FW_CAcquireLockedSystemHandle<tItem>::GetPointer() const
  282. {
  283.     return fLockedPointer;
  284. }
  285.  
  286. //========================================================================================
  287. // CLASS FW_CAcquireTemporarySystemHandle
  288. //
  289. //    A resource acquisition helper object for allocating and locking a system handle
  290. //  within a scope.  You can transfer ownership of the handle by setting fFree to FALSE.
  291. //========================================================================================
  292.  
  293. template<class tItem>
  294. class FW_CAcquireTemporarySystemHandle : public _FW_CAutoDestructObject
  295. {
  296. public:
  297.     FW_CAcquireTemporarySystemHandle(unsigned long size);
  298.     virtual ~FW_CAcquireTemporarySystemHandle();
  299.     
  300.     void Resize(unsigned long newSize);
  301.     
  302.     FW_PlatformHandle fTemporarySystemHandle;
  303.     tItem* fMemoryPointer;
  304.  
  305.     FW_Boolean fFree;                        // If TRUE, the dtor will free the fMemory
  306.  
  307. private:
  308.     FW_CAcquireTemporarySystemHandle(const FW_CAcquireTemporarySystemHandle<tItem>& acquireObject);
  309.     FW_CAcquireTemporarySystemHandle<tItem>& operator=(const FW_CAcquireTemporarySystemHandle<tItem>& acquireObject);
  310.         // Copy constructor and assignment operator not valid for this class.
  311. };
  312.  
  313. //----------------------------------------------------------------------------------------
  314. // FW_CAcquireTemporarySystemHandle::FW_CAcquireTemporarySystemHandle
  315. //----------------------------------------------------------------------------------------
  316.  
  317. template<class tItem>
  318. FW_CAcquireTemporarySystemHandle<tItem>::FW_CAcquireTemporarySystemHandle(unsigned long size) :
  319.     fTemporarySystemHandle(FW_CMemoryManager::AllocateSystemHandle(size))
  320. {
  321.     FW_TRY
  322.     {
  323.         fMemoryPointer = (tItem*) FW_CMemoryManager::LockSystemHandle(fTemporarySystemHandle);
  324.         fFree = TRUE;
  325.     }
  326.     FW_CATCH_BEGIN
  327.     FW_CATCH_EVERYTHING()
  328.     {
  329.         FW_CMemoryManager::FreeSystemHandle(fTemporarySystemHandle);
  330.         FW_THROW_SAME();
  331.     }
  332.     FW_CATCH_END
  333.     
  334.     FW_END_CONSTRUCTOR
  335. }
  336.  
  337. //----------------------------------------------------------------------------------------
  338. // FW_CAcquireTemporarySystemHandle::~FW_CAcquireTemporarySystemHandle
  339. //----------------------------------------------------------------------------------------
  340.  
  341. template<class tItem>
  342. FW_CAcquireTemporarySystemHandle<tItem>::~FW_CAcquireTemporarySystemHandle(void)
  343. {
  344.     FW_START_DESTRUCTOR
  345.     
  346.     FW_CMemoryManager::UnlockSystemHandle(fTemporarySystemHandle);
  347.         
  348.     if(fFree)
  349.         FW_CMemoryManager::FreeSystemHandle(fTemporarySystemHandle);
  350. }
  351.  
  352.  
  353. //----------------------------------------------------------------------------------------
  354. // FW_CAcquireTemporarySystemHandle::Resize
  355. //----------------------------------------------------------------------------------------
  356.  
  357. template<class tItem>
  358. void FW_CAcquireTemporarySystemHandle<tItem>::Resize(unsigned long newSize)
  359. {
  360.     // unlock the handle before resizing
  361.     FW_CMemoryManager::UnlockSystemHandle(fTemporarySystemHandle);
  362.     
  363.     // then resize it
  364.     FW_TRY
  365.     {
  366.         fTemporarySystemHandle = 
  367.             FW_CMemoryManager::ResizeSystemHandle(fTemporarySystemHandle, newSize);
  368.     }
  369.     FW_CATCH_BEGIN
  370.     FW_CATCH_EVERYTHING()
  371.     {
  372.         // relock the handle
  373.         FW_CMemoryManager::LockSystemHandle(fTemporarySystemHandle);
  374.         FW_THROW_SAME();
  375.     }
  376.     FW_CATCH_END
  377.     
  378.     FW_CMemoryManager::LockSystemHandle(fTemporarySystemHandle);
  379. }
  380.  
  381. #endif
  382.